const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const JQuery = require('jquery')
const Webpack = require('webpack')
const CopyPlugin = require("copy-webpack-plugin");

console.log("生产环境")

module.exports = {
    mode: 'production',
    output: {
        path: path.resolve(__dirname, '../dist'),
        filename: 'js/[name].bundle.[hash:8].js',
        clean: true, //如果开启的dll模式，需要关闭这句话
    },
    // watch: true,//开启监听模式
    // watchOptions: {
    //     poll: 1000,//每秒监听1000次，有保存就更新视图
    //     aggregateTimeout: 500,//防抖，间隔500毫秒保存时，不更新
    //     ignored:/node_modules/
    // },
    plugins: [

        // dll设置，如果需要开启，要先执行build_init，再执行build。就不要抽离公共代码了

        // new Webpack.DllReferencePlugin({
        //     //优先去这个位置找清单，真的找不到，再打包jquery
        //     manifest: path.resolve(__dirname, '../dist', 'manifest.json')
        // }),
        new Webpack.BannerPlugin({ //版权说明，uglyjs会删除注释，变成只在css中出现。。。。
            banner: 'hello world',
        }),
        new CopyPlugin({
            patterns: [
                { from: "./source", to: "./source" }, //将根路径下的source 复制到dist路径下
                // { from: "other", to: "public" },
            ],
        }),
        new Webpack.ProvidePlugin({
            //将jquery注入到每一个模块中
            '$': 'JQuery'
        }),
        new HtmlWebpackPlugin({
            template: './public/index.html',
            filename: 'index.html',
            chunks: ['index', 'self'],
            minify: {
                removeAttributeQuotes: true,
                collapseWhitespace: true
            }
        }),
        new HtmlWebpackPlugin({
            template: './public/page2.html',
            filename: 'page2.html',
            chunks: ['self'],
            minify: {
                removeAttributeQuotes: true,
                collapseWhitespace: true
            }
        }),

        new MiniCssExtractPlugin({
            filename: 'css/[name].css',
        })
    ],

    // 优化
    optimization: {
        minimize: true,
        minimizer: [
            // `...`,
            new CssMinimizerPlugin(),
            new UglifyJsPlugin()
        ],
        // 抽离公共代码块
        splitChunks: {
            cacheGroups: { //缓存组
                common: { //公共模块
                    name: 'common',
                    chunks: 'initial', //一开始就抽离
                    minSize: 0, //最小多少字节抽离
                    minChunks: 2 //引用多少次抽离,写0或写1的话，你就是憨憨
                },
                vendor: { //第三方库
                    test: /node_modules/, //查找路径
                    name: 'vendor',
                    priority: 1, //权重，不设置会直接被上面抽离
                    chunks: 'initial', //一开始就抽离
                    minSize: 0, //最小多少字节抽离
                    minChunks: 2 //引用多少次抽离,写0或写1的话，你就是憨憨
                }
            }
        }
    },
};